package NC;

import java.util.ArrayList;

public class NC38 {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> ans = new ArrayList<>();
        int rowstart = 0,rowend = matrix.length-1,colstart = 0,colend = matrix[0].length-1;
        while (rowstart<rowend&&colend>colstart){
            for (int i = colstart; i < colend; i++) {
                ans.add(matrix[rowstart][i]);
            }
            rowstart++;
            for(int i = rowstart;i<rowend;i++){
                ans.add(matrix[i][colend]);
            }
            colend--;
            if(rowstart<=rowend)  for( int i = colend; i > colstart; i--) {
                ans.add(matrix[rowend][i]);
            }
            rowend--;
            if(colstart<=colend) for (int i =rowend; i >rowstart ; i--) {
                ans.add(matrix[i][colstart]);
            }
            colstart++;
        }
        return ans;
    }
}